package com.ysh.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.ysh.domain.store.QuestionItem;
import com.ysh.utils.BeanUtil;
import com.ysh.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String operation = req.getParameter("operation");
        // 获取页面页码
        if ("list".equals(operation)){
            list(req, resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            edit(req,resp);
        }else if ("delete".equals(operation)){
            delete(req,resp);
        }else if("saveOrUpdate".equals(operation)){
            this.saveOrUpdate(req,resp);
        }

    }

    /*
        删除操作
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // 获取前台id ,并封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(req,QuestionItem.class);
        // 调用业务层接口save
        questionItemService.delete(questionItem);
        // 跳转回页面list
        list(req,resp);
    }




    /*
        跳转到修改页面,并带入数据
        根据id查询信息，并将结果带入页面中进行回显
     */
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 查询要修改的数据findById
        String id = req.getParameter("id");
        // 根据id查询结果
        QuestionItem questionItem = questionItemService.findById(id);
        // 将数据加载到指定区域，供页面获取
        req.setAttribute("questionItem",questionItem);
        // 调用list
        list(req,resp);
    }

    /*
       添加和修改操作
    */
    private void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // 将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(req,QuestionItem.class,"yyyy-MM-dd");

        if (StringUtils.isNotBlank(questionItem.getId())){
            // 有id传入，执行修改操作方法
            questionItemService.update(questionItem);
        }else{
            // 没有id传入，执行添加操作
            questionItemService.save(questionItem);
        }

        // 调用list
        list(req,resp);
    }

    /*
        修改操作
     */
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(req,QuestionItem.class,"yyyy-MM-dd");
        // 调用业务层接口
        questionItemService.update(questionItem);

        // 调用list
        list(req,resp);
    }
    /*
       添加操作
    */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(req,QuestionItem.class,"yyyy-MM-dd");
        // 调用业务层接口
        questionItemService.update(questionItem);

        // 跳转回页面list
        list(req,resp);
    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String questionId = req.getParameter("questionId");
        // 进入list页时添加对应的问题id，为添加操作使用
        req.setAttribute("questionId",questionId);
        // 获取数据，指定参数进行分页查询
        PageInfo all = questionItemService.findAll(questionId,1, 100);
        // 将数据保存到指定的位置
        req.setAttribute("page",all);
        // 跳转list页面
        req.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

}
